<%@page import="java.util.Calendar"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.text.DateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="org.hibernate.Query"%>
<%@page import="com.friends.library.persistence.HibernateUtil"%>
<%@page import="org.hibernate.Transaction"%>
<%@page import="org.hibernate.Session"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script>
function daysBetween(){
	var date1=document.getElementById("date1").value;
	
	var date2=document.getElementById("date2").value;
	   if (date1.indexOf("-") != -1) { date1 = date1.split("-"); } else if (date1.indexOf("/") != -1) { date1 = date1.split("/"); } else { return 0; } 
	   if (date2.indexOf("-") != -1) { date2 = date2.split("-"); } else if (date2.indexOf("/") != -1) { date2 = date2.split("/"); } else { return 0; } 
	   if (parseInt(date1[0], 10) >= 1000) { 
	       var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]); 
	   } else if (parseInt(date1[2], 10) >= 1000) { 
	       var sDate = new Date(date1[2]+"/"+date1[0]+"/"+date1[1]); 
	   } else { 
	       return 0; 
	   } 
	   if (parseInt(date2[0], 10) >= 1000) { 
	       var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]); 
	   } else if (parseInt(date2[2], 10) >= 1000) { 
	       var eDate = new Date(date2[2]+"/"+date2[0]+"/"+date2[1]); 
	   } else { 
	       return 0; 
	   } 
	   var one_day = 1000*60*60*24; 
	   var daysApart = Math.abs(Math.ceil((sDate.getTime()-eDate.getTime())/one_day)); 
	   if(daysApart<7){
		   var fine="0";
		  document.form.result.value=test;
	   }else{
		   daysApart=(daysApart-7)*3;
		  document.form.result.value=daysApart;
	   } 
	   return false;
	} 
</script>
</head>
<body>
<%
int status=1;
int bookid=(Integer)request.getAttribute("bookid");

Session session2=HibernateUtil.getSessionFactory().openSession();
Transaction transaction2=session2.beginTransaction();
String sql1="update BookRegistration set status=:status where bookid=:bookid";
Query query1=session2.createQuery(sql1);
query1.setInteger("status",status);
query1.setInteger("bookid",bookid);
//query.setInteger("status", status);
int result1=query1.executeUpdate();

transaction2.commit();

session2.close();
if(result1==0){
	out.println("Not Updated");
}else{
	out.println("updated successfully");
}
%>

<h3><a href="return.jsp">Back</a></h3>
<%
 

String DATE_FORMAT = "yyyy-MM-dd";
SimpleDateFormat sdf =
      new SimpleDateFormat(DATE_FORMAT);
Calendar c1 = Calendar.getInstance(); 
String date2=sdf.format(c1.getTime());

String date1=(String)request.getAttribute("date");


%>
<form action="Fine" name="form"  method="post" onSubmit="return daysBetween();">
<table>
<tr>
<td>Issued Date</td>
<td>
<input type="text" id="date1" name="date1" value="<%= date1 %>">
</td>
</tr>
<tr><td>Today Date</td>
<td>
<input type="text" id="date2" name="date2" value="<%= date2 %>"></td>
</tr>
<tr>
<td>Fine</td>
<td>
<input type="text" name="result" value=""></td>
</tr>
<tr><td>
</td>
<td>
<input type="submit" value="submit">
</td>
</tr>
</table>
</form>
</body>
</html>